package com.itheima.dao;

import com.itheima.model.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    //根据id查询用户
    @Select("SELECT * FROM t_user WHERE id=#{id}")
    public User selectUserWithId(Integer id);

    //统计用户数量
    @Select("SELECT COUNT(*) FROM t_user")
    public Integer getUserCount();

    //分页查询用户
    @Select("SELECT * FROM t_user")
    public List<User> selectUserWithPage();

    //查询用户权限
    @Select("SELECT a.authority FROM t_user u,t_authority a," +
            "t_user_authority ua WHERE ua.user_id=u.id " +
            "and ua.authority_id=a.id and u.id=#{id}")
    public String selectUserAuthority(int id);

    //根据id删除用户
    @Delete("DELETE t_user, t_user_authority\n" +
            "FROM t_user\n" +
            "JOIN t_user_authority ON t_user.id = t_user_authority.user_id\n" +
            "WHERE t_user.id=#{id}")
    public void deleteUserWithId(int id);

    //更新用户权限
    @Update("UPDATE t_user_authority ua\n" +
            "SET ua.authority_id=1\n" +
            "WHERE ua.user_id=#{id}" )
    public void updateAuthorityWithId(int id);

    //根据主键更新用户信息
    public Integer updateUserWithId(User user);

    //根据用户名搜索
    @Select(" SELECT * FROM t_user WHERE username LIKE CONCAT('%', #{keyword}, '%')")
    public List<User> findByUsernameContaining(String keyword);
}
